package com.block.datapush.service.apipush;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.IMemberCommonService;
import com.block.common.service.LogFactory;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.model.PushQueryParam;
import com.block.datapush.model.dto.NoticeParamDTO;
import com.block.datapush.model.result.ProtocolParamResult;
import com.block.datapush.model.result.PushApiResult;
import com.block.datapush.service.IPushApiService;
import com.block.datapush.service.apipush.utils.NingBoUtil;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 汇信全国
 */
@Service
public class HuiXinApiService implements IPushApiService {

    private static final Logger log = LogFactory.getPushLog();

    @Autowired
    IMemberCommonService memberCommonService;

    @Override
    public PushApiResult checkUser(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {

        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);


        String queryUrl = pushApiKeyParam.getCheckUrl();

        String mobileMd5 = memberSourceInfo.getMobileMd5();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("mobile", mobileMd5);
        paramMap.put("channel", "zxfq");


        log.info("【汇信全国】撞库开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【汇信全国】撞库结束【result】{}", result);


        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");


        pushApiResult.setCode(code + "");
        pushApiResult.setMessage(msg);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "1")) {

            JSONObject data = resultObject.getJSONObject("data");
            String orderNo1 = data.getString("orderNo");
            extJson.put("orderNo1", orderNo1);

            pushApiResult.setPushSuccess(true);
            return pushApiResult;
        }

        return pushApiResult;
    }

    @Override
    public PushApiResult queryApprove(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        pushApiResult.setOrderNo(orderNo);
        return pushApiResult;
    }

    @Override
    public PushApiResult registerUser(String orderNo, String outOrderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);

        String requestUrl = pushApiKeyParam.getRegisterUrl();


        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("name", memberSourceInfo.getRealname());
        bizMap.put("age", memberSourceInfo.getAge());
        bizMap.put("mobile", memberSourceInfo.getMobile());
        bizMap.put("idcard", memberSourceInfo.getIdcard());
        bizMap.put("sex", memberSourceInfo.getSex());
        bizMap.put("province", memberSourceInfo.getProvinceCode());
        bizMap.put("city", memberSourceInfo.getCityCode());
        bizMap.put("xueli", memberSourceInfo.getXueli());
        bizMap.put("loan_limit", memberSourceInfo.getLoanAmount());
        bizMap.put("loan_time", memberSourceInfo.getLoanTime());
        bizMap.put("yongtu", memberSourceInfo.getYongtu());
        bizMap.put("si", memberSourceInfo.getShebao());
        bizMap.put("cpf", memberSourceInfo.getGjj());
        bizMap.put("job", memberSourceInfo.getGongzuo());
        bizMap.put("car", memberSourceInfo.getCar() == "car001" ? "car001" : "car002");
        bizMap.put("house", memberSourceInfo.getHouse() == "hu001" ? "hu001" : "hu002");
        bizMap.put("insures", memberSourceInfo.getBaoxian() == "bx001" ? "bx001" : "bx002");
        bizMap.put("yuqi", memberSourceInfo.getYuqi() == "yq001" ? "yq001" : "yq002");
        bizMap.put("salary", memberSourceInfo.getShouru());
        bizMap.put("credit_card", memberSourceInfo.getXinyongka());
        bizMap.put("zmf", memberSourceInfo.getZhima());
        bizMap.put("huabei", memberSourceInfo.getHuabei());
        bizMap.put("baitiao", memberSourceInfo.getBaitiao());
        bizMap.put("wld", memberSourceInfo.getWeili());


        String orderNo1 = extJson.getString("orderNo1");

        String content = SecureUtil.aes(pushApiKeyParam.getApiSecret().getBytes()).encryptBase64(JSONObject.toJSONString(bizMap));

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orderNo", orderNo1);
        paramMap.put("tranData", content);
        paramMap.put("channel", "zxfq");


        log.info("【汇信全国】导单开始【requestUrl】{}【paramMap】{}【bizMap】{}", requestUrl, JSON.toJSONString(paramMap), JSON.toJSONString(bizMap));
        String result = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【汇信全国】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");


        pushApiResult.setCode(code + "");
        pushApiResult.setMessage(msg);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "1")) {
            pushApiResult.setPushSuccess(true);
            pushApiResult.setSettleFlag(true);
            pushApiResult.setStatus(PushStatusEnum.SUCCESS.getValue());
            return pushApiResult;
        }
        pushApiResult.setPushSuccess(false);
        pushApiResult.setSettleFlag(false);
        pushApiResult.setStatus(PushStatusEnum.FAILED.getValue());
        return pushApiResult;
    }

    @Override
    public PushApiResult queryResult(PushQueryParam pushQueryParam, PushApiKeyParam pushApiKeyParam, String extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        return pushApiResult;
    }

    @Override
    public NoticeParamDTO getNoticeResultParam(String infoId, Map<String, String[]> parameterMap, String paramContent) {
        NoticeParamDTO noticeParamDTO = new NoticeParamDTO();
        return noticeParamDTO;
    }

    @Override
    public JSONObject getNoticeResultReturn(Map<String, String[]> parameterMap, String paramContent, JSONObject extObject) {
        JSONObject jsonObject = new JSONObject();
        return jsonObject;
    }


    @Override
    public ProtocolParamResult getProtocolResult(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {
        return null;
    }


}
